package com.pageoffice.demo;

import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;

import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.pageoffice.util.SystemUtil;

/**
 * description: 打开简单word页面 
 * @version v1.0
 * @author w
 * @date 2018年12月2日下午2:17:58
 **/
@WebServlet(value={"/simple"})
public class SimpleWordServlet extends HttpServlet {
	
	private static final String SUFFIX = "WEB-INF/views";

	private static final String LINUX_PREFIX= "file://";
	
	/**
	 */
	private static final long serialVersionUID = 1L;
	
	@Override
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String basePath = request.getSession().getServletContext().getRealPath(File.separator);
		if(!basePath.endsWith("/")) {
			// fix: Tomcat9 时，会出现不加载 File.separator 的标记。
			basePath = basePath.concat("/");
		}
		String file = basePath+"WEB-INF/views/SimpleWord/doc/test.doc";
		//	String file = basePath+"test.doc";
		if(SystemUtil.isLinux()){
			// linux os need prefix : file://
			file = LINUX_PREFIX + file ;
		}
		request.setAttribute("file", file);
		request.getRequestDispatcher(SUFFIX+"/SimpleWord/Word.jsp").forward(request, response);
	}
	
	
	@SuppressWarnings("unused")
	private void writeJson(HttpServletRequest request, HttpServletResponse response)
			throws UnsupportedEncodingException, IOException {
		request.setCharacterEncoding("UTF-8");
		response.setCharacterEncoding("UTF-8");
		response.setContentType("application/json");
		
		PrintWriter out = response.getWriter();
		out.println("simple 简单的world");
		out.flush();
		out.close();
	}
	
	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse response) throws ServletException, IOException {
		doGet(req, response);
	}

}
